package com.xiaohui.user.controller;

import com.xiaohui.com.xiaohui.bean.Result;
import com.xiaohui.entity.PageResult;
import com.xiaohui.user.entity.dto.UserDto;
import com.xiaohui.user.entity.req.UserReq;
import com.xiaohui.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping
    @ ApiOperation(value = "新增用户")
    public Result addUser(@RequestBody UserReq userReq){
        UserDto dto = new UserDto();
        BeanUtils.copyProperties(userReq, dto);
        return Result.ok(userService.addUser(dto));
    }

    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Long id){
        return Result.ok(userService.delete(id));
    }

    @GetMapping
    public PageResult selectByPage(@RequestBody UserReq userReq){
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userReq, userDto);
        return userService.selectByPage(userDto);
    }
}
